
KISSY.ready(function(S) {
	var Dom = S.DOM, Event = S.Event;
	/**
	 * class: myTimer
	 * @author: qipbbn@gmail.com
	 * 一个简易的定时器
	 */
    function myTimer(config) {
		var t = this;
        t.config = config || {};
		t.el_hour = S.get(config.c_hour);
		t._setting = {};
		
		t.el_min = S.get(config.c_min);
		//console.log(t.el_min);
		t.el_c = S.get(config.c_container);
		t.el_btn = S.get(config.c_btn);
		t.el_lcd = S.get(config.c_lcd);
		t.setSelect(t.el_hour, 60);
		t.setSelect(t.el_min, 60);
		
		t._running = false;
		t._timeup = false; //是否定时结束
		
		if(typeof Audio !== "undefined"){
			t._audio_alerm = new Audio("sound/Ding.ogg");
			t._audio_alerm.preload = true;
			t._audio_alerm.loop = true;
		}else{
			t._audio_alerm = {
				play : function(){},
				pause: function(){}
			};
		}
		
		
		t._timeout_anim = S.Anim(t.el_lcd, "color:#ff0000", .5 , S.Easing.easeIn,function(){
			Dom.css(t.el_lcd,"color","#ffffff");
			t._timeout_anim.run();
		});
		
		t.setup();
    };

	S.mix(myTimer.prototype,{
		
		setSelect : function(el,n){
			el.innerHTML = '';
			for(var i=0; i < n; i++){
				var opt = document.createElement('option');
				opt.text = i;
				opt.value = i
				try{
					el.add(opt,null); // standards compliant
				} catch(ex){
					el.add(opt); // IE only
				}
			}
		},
		
		setup : function(){
			var t = this;
			Event.on(this.el_btn, "click", function(){
				t.btnClick();
			});
		},
		
		btnClick : function(){
			var t = this;
			//console.log(t);
			if(!t._running){
				t.start();
			} else {
				if(t._timeup) {
					//Dom.get("#Sound_ding").pause();
					t._audio_alerm.pause();
					t._timeup = false;
					Dom.css(t.el_lcd, "color", "#fff");
					
				}else{
					
				}
				clearTimeout(t._timeout);
				t.stop();
			}
 		},
		
		start : function(){
			//console.log("start",this.el_c);
			var t = this,  setting, remain;
			Dom.addClass(t.el_c,"running");
			t.el_btn.innerHTML = "Stop";
			t._running = true;
			setting = {
				start : new Date().getTime(),
				h : parseInt(this.el_hour.options[this.el_hour.selectedIndex].value),
				m : parseInt(this.el_min.options[this.el_min.selectedIndex].value)
			};
			setting.end = setting.start + t.getTime(setting);
			t._setting = setting;
			
			//console.dir(t._setting);
			//console.log("end - start", setting.end - setting.start);
			//console.log("start at ",new Date(setting.start))
			//console.log("end at ",new Date(setting.end));
			t.everySecond();
		},
		
		everySecond : function(){
			var t = this;
			var date = new Date(),
				remain = Math.max(t._setting.end - date.getTime(),0);
			t.updateLCD(remain);
			if(remain < 500 ){
				//clear(t._timeout);
				t._timeup = true;
				//console.log("play");
				//t._timeout_anim.run();
				//Dom.get("#Sound_ding").play();
				t._audio_alerm.play();
				t.stop();
				alert("时间到");
				t._audio_alerm.pause();
			}else{
				t._timeout = setTimeout(function(){
					t.everySecond();
				},1000);
			}
		},
		
		updateLCD : function(remain){
			var sec = Math.round(remain/1000),
				min = Math.floor(sec/60),
				display;
			sec = sec%60;
			sec = sec < 10?"0"+sec:sec;
			//display = min>0? min+"分":"";
			//this.el_lcd.innerHTML = display + sec + "秒";
			Dom.html(this.el_lcd,min+":"+sec);
		},
		
		stop : function (){
			var t = this;
			//t._timeout_anim.stop();
			Dom.removeClass(t.el_c, "running")
			this.el_btn.innerHTML = "Start";
			t._running = false;
			t._timeup = false;
		},
		
		getTime : function(s){
			//return s.h*60*60 + s.m*60;
			return 1000*(s.h*60 + s.m);//ms
		}
	});
    var cls = new myTimer({
		c_container : "#clock_c",
		c_hour : "#clock_hour",
		c_min : "#clock_min",
		c_btn : "#timer_btn",
		c_lcd : "#timer_lcd"
	});
	
	setTimeout(function(){
		//console.log("btn click;");
		var min = S.get("#clock_min");
		//min.selectedIndex = "3";
		
		var norb_m = new Clock.Knob({
			el_sel : "#clock_min",
			container : "#min_c",
			unit : "秒"
		});
		norb_m.goTo(1);
		var norb_h = new Clock.Knob({
			el_sel : "#clock_hour",
			container : "#hour_c",
			unit : "分钟"
		});
		setTimeout(function(){
			//alert("start");
			//S.get("#timer_btn").click();
		});
	}, 1000);
});